#include"http_hand.h"
#include <arpa/inet.h>
#include <cstdio>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/socket.h>
#include <thread>
int main(){
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if (sfd==-1) {
		perror("socket");
		return -1;
	}
	struct sockaddr_in saddr;
	saddr.sin_addr.s_addr=inet_addr("192.168.234.128");
	saddr.sin_port=htons(80);
	saddr.sin_family=AF_INET;
	if(bind(sfd,(struct sockaddr *)&saddr,sizeof(saddr))==-1){
		perror("bind");
		return -1;
	}
	if(listen(sfd,128)==-1){
		perror("listen");
		return -1;
	}
	while (1) {
		struct sockaddr_in addr;
		socklen_t len;
		int fd=accept(sfd, (struct sockaddr *)&addr,&len);
		if (fd==-1) {
			perror(inet_ntoa(addr.sin_addr));
			continue;
		}
		int rcvBufSize =(1*1024*1024+4096)/2;//缓冲区大小为1MB+4kb
		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvBufSize, sizeof(rcvBufSize)) < 0)
		{
			printf("setsockopt error=%d(%s)!!!\n", errno, strerror(errno));
			continue;
		}
		int rcv_size=0;
		socklen_t optlen = sizeof(rcv_size); 
		if(getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcv_size, &optlen)<0){ 
			printf("获取接收缓冲区大小错误\n"); 
		} 
		cout<<"rcv_size:"<<rcv_size<<endl;
		printf("与%s成功建立连接\n",inet_ntoa(addr.sin_addr));
		thread Th(http_connect,fd);
		Th.detach();
	}
	cout<<1<<endl;
	return 0;
}